package bscorp.appbase;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.support.ConnectionSource;

import java.util.ArrayList;
import java.util.List;

import bscorp.appbase.db.M4DatabaseHelper;
import bscorp.appbase.model.Device;
import bscorp.appbase.model.Operate;
import bscorp.appbase.model.Order;
import bscorp.appbase.model.User;

public class AppManager extends Application {

    public static Context context;
    public static Resources resources;
    public static SharedPreferences preferences;
    public static User LOCAL_LOGINED_USER;
    public static final String BUNDLE_TYPE = "BUNDLE_TYPE";

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        resources = getResources();
        preferences = getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
        initDatabase();
    }

    private void initDatabase() {
        M4DatabaseHelper.M4DataBaseListener dataBaseListener = new M4DatabaseHelper.M4DataBaseListener() {
            @Override
            public void databaseUpdate(SQLiteDatabase db, ConnectionSource arg1, int oldVersion, int newVersion) {
                if (oldVersion == 1) {
                    db.execSQL("ALTER TABLE Operate ADD COLUMN userPhone varchar(128)");
                }
            }

            @Override
            public List<Class<?>> getCreatTableList() {
                List retList = new ArrayList();
                retList.add(Device.class);
                retList.add(Operate.class);
                retList.add(Order.class);
                retList.add(User.class);
                return retList;
            }
        };
        M4DatabaseHelper.getInstance(context);
        M4DatabaseHelper.getInstance(context).setDatabaseListener(dataBaseListener);
    }

    public static boolean isLogined() {
        return LOCAL_LOGINED_USER != null;
    }

}
